package operation;


import JDBC.JDBCUtils;
import Medicine.Pharmacy;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.SQLException;
import java.util.Scanner;

public class ChangeBuyPassword implements IOPeration {
    public void work(Pharmacy pharmacy) throws SQLException {

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要修改的账号: ");
        String user = sc.nextLine();
        System.out.println("请输入新的密码: ");
        String newPassword = sc.nextLine();
        updatePassword(user, newPassword);
    }

    public static void updatePassword(String user, String newPassword) throws SQLException {
        QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
        String sql = "UPDATE buyman SET Password=? WHERE USER=?";
        Object[] params = {newPassword, user};
        int count = qr.update(sql, params);
        if (count < 1) {
            System.out.println("修改失败，没有找到对应的账号或更新不成功。");
        } else {
            System.out.println("修改密码成功！");
        }
    }
}
